#coding=utf-8
#Boa:FramePanel:PanelDeliveryPlanList

import wx

from wx.lib.splitter import MultiSplitterWindow

from ctrlAider import *
from MyListCtrl import MyListCtrlDeliveryPlan,MyListCtrlDeliverySaleOrder,MyOrderContentListCtrl
from product import *

from PanelDeliveryPlanContent import PanelDeliveryPlanContent

[wxID_PANELDELIVERYPLANLIST, wxID_PANELDELIVERYPLANLISTBTNALLDATE, 
 wxID_PANELDELIVERYPLANLISTBTNCHECK, wxID_PANELDELIVERYPLANLISTBTNCHECKMALL, 
 wxID_PANELDELIVERYPLANLISTBTNDAYAFTER, 
 wxID_PANELDELIVERYPLANLISTBTNDAYBEFORE, wxID_PANELDELIVERYPLANLISTBTNDONE, 
 wxID_PANELDELIVERYPLANLISTBTNNEEDPIC, wxID_PANELDELIVERYPLANLISTBTNONEDAY, 
 wxID_PANELDELIVERYPLANLISTBTNONEMONTH, wxID_PANELDELIVERYPLANLISTBTNONEWEEK, 
 wxID_PANELDELIVERYPLANLISTBTNONEYEAR, wxID_PANELDELIVERYPLANLISTBTNPLANING, 
 wxID_PANELDELIVERYPLANLISTBTNSENDING, wxID_PANELDELIVERYPLANLISTBUTTON1, 
 wxID_PANELDELIVERYPLANLISTCOMBODRIVER, wxID_PANELDELIVERYPLANLISTCOMBOSTATE, 
 wxID_PANELDELIVERYPLANLISTCOMBOTRUCK, wxID_PANELDELIVERYPLANLISTDATEEND, 
 wxID_PANELDELIVERYPLANLISTDATESTART, wxID_PANELDELIVERYPLANLISTSTATICTEXT1, 
 wxID_PANELDELIVERYPLANLISTSTATICTEXT2, wxID_PANELDELIVERYPLANLISTSTATICTEXT3, 
 wxID_PANELDELIVERYPLANLISTSTATICTEXT4, wxID_PANELDELIVERYPLANLISTTSTATE, 
] = [wx.NewId() for _init_ctrls in range(25)]

class PanelDeliveryPlanList(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELDELIVERYPLANLIST,
              name=u'PanelDeliveryPlanList', parent=prnt, pos=wx.Point(480,
              144), size=wx.Size(1061, 702), style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1045, 664))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.comboState = wx.ComboBox(choices=[],
              id=wxID_PANELDELIVERYPLANLISTCOMBOSTATE, name=u'comboState',
              parent=self, pos=wx.Point(126, 48), size=wx.Size(150, 24),
              style=wx.CB_DROPDOWN | wx.CB_READONLY, value=u'')
        self.comboState.SetLabel(u'')
        self.comboState.Bind(wx.EVT_COMBOBOX, self.OnComboStateCombobox,
              id=wxID_PANELDELIVERYPLANLISTCOMBOSTATE)

        self.tState = wx.StaticText(id=wxID_PANELDELIVERYPLANLISTTSTATE,
              label=u'\u8f66\u8f86\uff1a', name=u'tState', parent=self,
              pos=wx.Point(848, 56), size=wx.Size(51, 16), style=0)

        self.btnPlaning = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNPLANING,
              label=u'\u8ba1\u5212\u4e2d', name=u'btnPlaning', parent=self,
              pos=wx.Point(296, 48), size=wx.Size(72, 26), style=0)
        self.btnPlaning.Bind(wx.EVT_BUTTON, self.OnBtnPlaningButton,
              id=wxID_PANELDELIVERYPLANLISTBTNPLANING)

        self.btnSending = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNSENDING,
              label=u'\u51fa\u8f66\u4e2d', name=u'btnSending', parent=self,
              pos=wx.Point(376, 48), size=wx.Size(72, 26), style=0)
        self.btnSending.Bind(wx.EVT_BUTTON, self.OnBtnSendingButton,
              id=wxID_PANELDELIVERYPLANLISTBTNSENDING)

        self.btnDone = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNDONE,
              label=u'\u5df2\u5b8c\u6210', name=u'btnDone', parent=self,
              pos=wx.Point(744, 48), size=wx.Size(72, 26), style=0)
        self.btnDone.Bind(wx.EVT_BUTTON, self.OnBtnDoneButton,
              id=wxID_PANELDELIVERYPLANLISTBTNDONE)

        self.comboDriver = wx.ComboBox(choices=[],
              id=wxID_PANELDELIVERYPLANLISTCOMBODRIVER, name=u'comboDriver',
              parent=self, pos=wx.Point(912, 16), size=wx.Size(102, 24),
              style=0, value=u'\u5168\u90e8\u53f8\u673a')
        self.comboDriver.SetLabel(u'\u5168\u90e8\u53f8\u673a')

        self.comboTruck = wx.ComboBox(choices=[],
              id=wxID_PANELDELIVERYPLANLISTCOMBOTRUCK, name=u'comboTruck',
              parent=self, pos=wx.Point(912, 48), size=wx.Size(102, 24),
              style=0, value=u'\u5168\u90e8\u8f66\u8f86')
        self.comboTruck.SetLabel(u'\u5168\u90e8\u8f66\u8f86')

        self.button1 = wx.Button(id=wxID_PANELDELIVERYPLANLISTBUTTON1,
              label=u'\u663e\u793a\u5168\u90e8', name='button1', parent=self,
              pos=wx.Point(32, 112), size=wx.Size(93, 26), style=0)
        self.button1.Show(False)
        self.button1.Bind(wx.EVT_BUTTON, self.OnBtnShowAllButton,
              id=wxID_PANELDELIVERYPLANLISTBUTTON1)

        self.staticText1 = wx.StaticText(id=wxID_PANELDELIVERYPLANLISTSTATICTEXT1,
              label=u'\u81f3', name='staticText1', parent=self,
              pos=wx.Point(254, 22), size=wx.Size(18, 18), style=0)

        self.dateStart = wx.DatePickerCtrl(id=wxID_PANELDELIVERYPLANLISTDATESTART,
              name=u'dateStart', parent=self, pos=wx.Point(126, 14),
              size=wx.Size(120, 27), style=wx.DP_DROPDOWN)

        self.dateEnd = wx.DatePickerCtrl(id=wxID_PANELDELIVERYPLANLISTDATEEND,
              name=u'dateEnd', parent=self, pos=wx.Point(278, 14),
              size=wx.Size(116, 27), style=wx.DP_DROPDOWN)

        self.btnOneDay = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNONEDAY,
              label=u'\u5f53\u65e5', name=u'btnOneDay', parent=self,
              pos=wx.Point(402, 14), size=wx.Size(46, 29), style=0)
        self.btnOneDay.Bind(wx.EVT_BUTTON, self.OnBtnOneDayButton,
              id=wxID_PANELDELIVERYPLANLISTBTNONEDAY)

        self.btnOneWeek = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNONEWEEK,
              label=u'\u4e00\u5468', name=u'btnOneWeek', parent=self,
              pos=wx.Point(450, 14), size=wx.Size(46, 29), style=0)
        self.btnOneWeek.Bind(wx.EVT_BUTTON, self.OnBtnOneWeekButton,
              id=wxID_PANELDELIVERYPLANLISTBTNONEWEEK)

        self.btnOneMonth = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNONEMONTH,
              label=u'\u4e00\u6708', name=u'btnOneMonth', parent=self,
              pos=wx.Point(498, 14), size=wx.Size(46, 29), style=0)
        self.btnOneMonth.Bind(wx.EVT_BUTTON, self.OnBtnOneMonthButton,
              id=wxID_PANELDELIVERYPLANLISTBTNONEMONTH)

        self.btnOneYear = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNONEYEAR,
              label=u'\u4e00\u5e74', name=u'btnOneYear', parent=self,
              pos=wx.Point(546, 14), size=wx.Size(46, 29), style=0)
        self.btnOneYear.Bind(wx.EVT_BUTTON, self.OnBtnOneYearButton,
              id=wxID_PANELDELIVERYPLANLISTBTNONEYEAR)

        self.btnAllDate = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNALLDATE,
              label=u'\u5168\u90e8', name=u'btnAllDate', parent=self,
              pos=wx.Point(594, 14), size=wx.Size(46, 29), style=0)
        self.btnAllDate.Bind(wx.EVT_BUTTON, self.OnBtnAllDateButton,
              id=wxID_PANELDELIVERYPLANLISTBTNALLDATE)

        self.btnDayBefore = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNDAYBEFORE,
              label=u'\u524d\u4e00\u5929', name=u'btnDayBefore', parent=self,
              pos=wx.Point(656, 14), size=wx.Size(64, 29), style=0)
        self.btnDayBefore.Bind(wx.EVT_BUTTON, self.OnBtnDayBeforeButton,
              id=wxID_PANELDELIVERYPLANLISTBTNDAYBEFORE)

        self.btnDayAfter = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNDAYAFTER,
              label=u'\u540e\u4e00\u5929', name=u'btnDayAfter', parent=self,
              pos=wx.Point(720, 14), size=wx.Size(64, 29), style=0)
        self.btnDayAfter.Bind(wx.EVT_BUTTON, self.OnBtnDayAfterButton,
              id=wxID_PANELDELIVERYPLANLISTBTNDAYAFTER)

        self.staticText2 = wx.StaticText(id=wxID_PANELDELIVERYPLANLISTSTATICTEXT2,
              label=u'\u72b6\u6001\uff1a', name='staticText2', parent=self,
              pos=wx.Point(24, 56), size=wx.Size(51, 16), style=0)

        self.staticText3 = wx.StaticText(id=wxID_PANELDELIVERYPLANLISTSTATICTEXT3,
              label=u'\u67e5\u8be2\u65e5\u671f\uff1a', name='staticText3',
              parent=self, pos=wx.Point(24, 24), size=wx.Size(85, 16), style=0)

        self.staticText4 = wx.StaticText(id=wxID_PANELDELIVERYPLANLISTSTATICTEXT4,
              label=u'\u53f8\u673a\uff1a', name='staticText4', parent=self,
              pos=wx.Point(848, 24), size=wx.Size(51, 16), style=0)

        self.btnCheck = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNCHECK,
              label=u'\u6574\u7406\u4e2d', name=u'btnCheck', parent=self,
              pos=wx.Point(456, 48), size=wx.Size(80, 26), style=0)
        self.btnCheck.Bind(wx.EVT_BUTTON, self.OnBtnCheckButton,
              id=wxID_PANELDELIVERYPLANLISTBTNCHECK)

        self.btnCheckMall = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNCHECKMALL,
              label=u'\u6574\u7406-\u7535\u5546', name=u'btnCheckMall',
              parent=self, pos=wx.Point(544, 48), size=wx.Size(93, 26),
              style=0)
        self.btnCheckMall.Bind(wx.EVT_BUTTON, self.OnBtnCheckMallButton,
              id=wxID_PANELDELIVERYPLANLISTBTNCHECKMALL)

        self.btnNeedPic = wx.Button(id=wxID_PANELDELIVERYPLANLISTBTNNEEDPIC,
              label=u'\u5f85\u622a\u56fe', name=u'btnNeedPic', parent=self,
              pos=wx.Point(640, 48), size=wx.Size(93, 26), style=0)
        self.btnNeedPic.Bind(wx.EVT_BUTTON, self.OnBtnNeedPicButton,
              id=wxID_PANELDELIVERYPLANLISTBTNNEEDPIC)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.cbState = MyComboBoxDecorator(self.comboState, CDataDeliveryState, CDataDeliveryState.fi_name)
        self.cbState.refresh()
        
        self.cbTruck = MyComboBoxDecorator(self.comboTruck, CDataTruck, CDataTruck.fi_id)
        self.cbTruck.refresh()
            
        self.cbDriver = MyComboBoxDecorator(self.comboDriver, CDataStaff, CDataStaff.COL_NAME, only_driver)
        self.cbDriver.refresh()
        
        self.splitterWin = MultiSplitterWindow(self, style=wx.SP_LIVE_UPDATE)
        
        self.lcPlan = MyListCtrlDeliveryPlan(self.splitterWin)
        #self.lcPlan.Move(wx.Point(10, 40))
        #self.lcPlan.SetSize(wx.Size(950, 150))
        self.lcPlan.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
        
        # self.lcBill = MyListCtrlDeliverySaleOrder(self.splitterWin)
        # #self.lcBill.Move(wx.Point(10, 200))
        # #self.lcBill.SetSize(wx.Size(950, 200))
        # self.lcBill.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnBillItemSelected)
        
        # self.lcContent = MyOrderContentListCtrl(self.splitterWin)
        # #self.lcContent.Move(wx.Point(10, 410))
        # #self.lcContent.SetSize(wx.Size(950, 200))
        
        self.lcPlan.SetMinSize(wx.Size(950, 50))
        self.splitterWin.AppendWindow(self.lcPlan, 120)
        
        # self.lcBill.SetMinSize(wx.Size(950, 50))
        # self.splitterWin.AppendWindow(self.lcBill, 200)
        
        # self.lcContent.SetMinSize(wx.Size(950, 50))
        # self.splitterWin.AppendWindow(self.lcContent)
        self.nb = PanelDeliveryPlanContent(self.splitterWin)
        self.nb.SetMinSize(wx.Size(950, 50))
        self.splitterWin.AppendWindow(self.nb)
        
        self.splitterWin.Move(wx.Point(16, 80))
        # 设置大小为什么必须放在最后？放在AppendWindow之前导致sashPos参数不起作用
        self.splitterWin.SetSize(wx.Size(1000, 570))
        self.splitterWin.SetOrientation(wx.VERTICAL)
        self.splitterWin.SizeWindows()
        
        self.OnBtnPlaningButton(None)
    
    # def OnBillItemSelected(self, event):
        # '''按照选中订单，显示订单内'''
        # item = self.lcBill.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        # billId = self.lcBill.getBillId(item)
        # self.lcContent.refresh({'billId':billId})
        
    def OnItemSelected(self, event):
        planId = self.lcPlan.getPlanId()
        #self.lcBill.refresh({'deliveryPlan':planId})
        self.nb.setData(planId)
        
    def OnBtnNewQuickButton(self, event):
        event.Skip()

    def refreshPlanList(self):
        filter = {}
                    
        obj = self.cbState.getSelectObj()
        if (obj is not None):
            filter['state'] = obj.getKeyVal()
            
        obj = self.cbTruck.getSelectObj()
        if (obj is not None):
            filter['truck'] = obj.getKeyVal()
            
        obj = self.cbDriver.getSelectObj()
        if (obj is not None):
            filter['driver'] = obj.getKeyVal()
        
        if (filter.has_key('state') and filter['state'] == 3):
            filter['dateStart'] = self.dateStart.GetValue().Format('%Y-%m-%d')
            filter['dateEnd'] = self.dateEnd.GetValue().Format('%Y-%m-%d')
        
        self.lcPlan.refresh(filter)
        
    def OnComboStateCombobox(self, event):
        self.refreshPlanList()

    def OnBtnShowAllButton(self, event):
        self.cbState.SetValue(u'')
        #self.lcPlan.refresh()
        self.refreshPlanList()

    def OnBtnPlaningButton(self, event):
        self.cbState.SetValue(self.btnPlaning.GetLabel())
        self.refreshPlanList()
        #event.Skip()

    def OnBtnSendingButton(self, event):
        self.cbState.SetValue(self.btnSending.GetLabel())
        self.refreshPlanList()
        #event.Skip()

    def OnBtnDoneButton(self, event):
        self.cbState.SetValue(self.btnDone.GetLabel())
        self.refreshPlanList()
        #event.Skip()

    def OnBtnOneDayButton(self, event):
        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())
        self.refreshPlanList()

    def OnBtnOneWeekButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Days(7)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)
        
        self.refreshPlanList()
        

    def OnBtnOneMonthButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Months(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)
        
        self.refreshPlanList()

    def OnBtnOneYearButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Years(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)
        
        self.refreshPlanList()

    def OnBtnAllDateButton(self, event):
        self.dateEnd.SetValue(wx.DateTime.Today())        
        self.dateStart.SetValue(wx.DateTimeFromDMY(1,0,2000,0,0,00))
        
        self.refreshPlanList()

    def OnBtnDayBeforeButton(self, event):
        day = self.dateEnd.GetValue()        
        span = wx.DateSpan.Days(1)
        day.SubtractDS(span)
        self.dateEnd.SetValue(day)
        self.dateStart.SetValue(day)
        
        self.refreshPlanList()

    def OnBtnDayAfterButton(self, event):
        day = self.dateEnd.GetValue()        
        span = wx.DateSpan.Days(1)
        day.AddDS(span)
        self.dateEnd.SetValue(day)
        self.dateStart.SetValue(day)
        
        self.refreshPlanList()

    def OnBtnCheckButton(self, event):
        self.cbState.SetValue(self.btnCheck.GetLabel())
        self.refreshPlanList()

    def OnBtnCheckMallButton(self, event):
        self.cbState.SetValue(self.btnCheckMall.GetLabel())
        self.refreshPlanList()

    def OnBtnNeedPicButton(self, event):
        self.cbState.SetValue(self.btnNeedPic.GetLabel())
        self.refreshPlanList()
